//
//  QYXPicOnlyViewController.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXPicOnlyViewController.h"
#import "QYXPicTableViewVell.h"
#import "QYXFindMoreModel.h"

@interface QYXPicOnlyViewController ()<UITableViewDelegate, UITableViewDataSource>
/** TableView */
@property (nonatomic, strong) UITableView *tableView;

/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation QYXPicOnlyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"专题";
    UIImage *image = [[UIImage imageNamed:@"back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self creatTableView];
    [self requiredData];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 *KFitHeight)];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"Lobster 1.4" size:15];
    label.text = @"- The End -";
    self.tableView.tableFooterView = label;
}



#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 240 *KFitHeight;
    self.tableView.sectionFooterHeight = 40 *KFitHeight;
    [self.tableView registerClass:[QYXPicTableViewVell class] forCellReuseIdentifier:@"QYXPicTableViewVell"];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QYXPicTableViewVell *cell = [tableView dequeueReusableCellWithIdentifier:@"QYXPicTableViewVell" forIndexPath:indexPath];
    QYXFindMoreModel *model = _dataArr[indexPath.row];
    [cell.pic sd_setImageWithURL:[NSURL URLWithString:model.image]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}


#pragma mark - 请求数据


- (void)requiredData
{

    [QYXNetTool getNetWithUrl:@"http://baobab.wandoujia.com/api/v3/specialTopics?_s=c878259d885b82afb27dc8b153480bf5&f=iphone&net=wifi&num=20&u=ebe4ca1f9d83e4dc4f915b0495e6e1946ee0eb93&v=2.2.0&vc=721" urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
        self.dataArr = [NSMutableArray array];
        for (NSDictionary *d in result[@"itemList"]) {
            QYXFindMoreModel *model = [[QYXFindMoreModel alloc] init];
            [model setValuesForKeysWithDictionary:d[@"data"]];
            [self.dataArr addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
//        NSLog(@"----%@", _dataArr);
    } failure:^(NSError *error) {
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
